#!/bin/bash
#
#********************************************************
#Author:                            jiangkang
#Date:                              2024-08-26
#FileName:                          start.sh
#Description:                       yum更新脚本
#********************************************************

install_user_home=$1

function Sed()
{
  sed  -i 's/mirror.centos.org/vault.centos.org/g' /etc/yum.repos.d/*.repo
  sed  -i 's/^#.*baseurl=http/baseurl=http/g' /etc/yum.repos.d/*.repo
  sed  -i 's/^mirrorlist=http/#mirrorlist=http/g' /etc/yum.repos.d/*.repo
  echo "第一步yum源配置成功!"
}

function InstallEnv()
{
  # 检查操作系统版本是否ok
  version_ok=`uname -a | awk '{ if (index($0,"el7.x86_64") > 0) print 1;else print 0; }'`
  if [ ${version_ok} -eq 0 ]; then
    echo "操作系统版本不支持!目前只支持centos7 x86_64!"
    exit 1
  fi

  # 提前更换yum源配置
  Sed

  #安装git
  git --version > /dev/null
  if [ $? -ne 0 ]; then
    echo "未安装git,尝试安装git"
    yum install -y git
  fi
  git --version > /dev/null
  if [ $? -ne 0 ]; then
    echo "git 安装失败!"
  fi
}

function Operation()
{
  git clone https://gitee.com/lpsdz-ybhdsg-jk/yum-source-update.git
  if [ $? -ne 0 ]; then 
    echo "git clone 失败!"
    exit 1
  fi
  echo "git clone 下载完成!"
  
  # 正式操作文件
  tar --version > /dev/null 2>&1
  if [ $? -ne 0 ]; then
    echo "tar命令没有,安装tar命令!"
    yum install -y tar
  fi

  #解压文件
  tar -xzvf ${install_user_home}/yum-source-update/bag.gz -C ${install_user_home}/yum-source-update
  mv -f ${install_user_home}/yum-source-update/bag/* /etc/yum.repos.d

  # 删除痕迹
  if [ -d ${install_user_home}/yum-source-update ];then
    rm -rf ${install_user_home}/yum-source-update
  fi

  if [ -f ${install_user_home}/install.sh ]; then
    rm -rf ${install_user_home}/install.sh
  fi
  
  # 更新yum源
  yum clean all && yum makecache
}

# 1.检查并安装依赖软件
InstallEnv
Operation
